package com.face.controller;

import com.alibaba.fastjson.JSON;
import com.face.common.Result;
import com.face.common.ResultCode;
import com.face.entity.Shop;
import com.face.entity.Visit;
import com.face.exception.DatabaseException;
import com.face.exception.NoAccountException;
import com.face.exception.NoShopException;
import com.face.service.AccountService;
import com.face.service.ShopService;
import com.face.util.StringConstans;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.lang.Nullable;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.sql.Timestamp;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/login")
public class LoginController {

    @Autowired
    private ShopService shopService;

    @Autowired
    private AccountService accountService;


    @PostMapping("/shopLogin")
    public Result shopLogin(@RequestBody String jsonData) {

        Map<String, String> map = JSON.parseObject(jsonData, HashMap.class);

        String shopId = map.get("shopId");
        String deviceId = map.get("deviceId");
        try {
            if (shopId == null || shopId.equals("") || deviceId == null || deviceId.equals("")) {
                return Result.failure(ResultCode.BAD_PARAMETER);
            }

            Shop shop = shopService.getShop(shopId, deviceId);
        } catch (NoShopException e) {
            return Result.failure(ResultCode.NO_ACCOUNT);
        }
        return Result.success(ResultCode.SUCCESS, shopId);
    }

    @PostMapping("/visit")
    public Result visit(@RequestBody Visit visit) {

        try {
            Date date = new Date();
            visit.setLoginTime(new Timestamp(date.getTime()));
            shopService.saveVisit(visit);
        } catch (DatabaseException e) {
            return Result.failure(ResultCode.CREATE_FAIL);
        }
        return Result.success(ResultCode.CREATED);
    }


    /**
     * 微信绑定
     *
     * @param openid
     * @param jsonData
     * @return
     */
    @PostMapping("/band")
    public Result band(@CookieValue(StringConstans.WECHAT_OPENID) String openid, @RequestBody String jsonData) {

        if (StringUtils.isEmpty(openid)) {
            return Result.failure(ResultCode.NOT_ENOUGH_PARAMETER);
        }
        String name = accountService.findUsernameByOpenid(openid);
        if (name != null && !StringUtils.isEmpty(name)) {
            return Result.failure(ResultCode.WECHAT_ACCOUNT_EXIST);
        }

        try {
            Map<String, String> map = JSON.parseObject(jsonData, HashMap.class);

            String username = map.get("username");
            String password = map.get("password");
            accountService.findByUsernameAndPassword(username, password);
            accountService.updateAccountWeixin(username, openid);
        } catch (NoAccountException e) {
            return Result.failure(ResultCode.USERNAME_OR_PASSWORD_ERROR);
        }

        return Result.success();
    }

}
